import { CartDataList, RemoveCartList, ChangChecked } from '@/api/cart';
import { getUUID } from '@/utils/uuid_token';
export default {
	// 开启命名空间
	namespaced: true,
	state: {
		// 购物车数据
		cartDataList: [],
		uuid_token: getUUID(),
	},
	mutations: {
		cartDataObj(state, obj) {
			state.cartDataList = obj;
		},
	},
	actions: {
		// 获取购物车数据列表
		async getCartDataList(context) {
			let result = await CartDataList();
			if (result.data.code === 200) {
				context.commit('cartDataObj', result.data.data);
			}
		},
		// 删除购物车商品
		async removeCartList(context, skuId) {
			let result = await RemoveCartList(skuId);
			if (result.data.code === 200) {
				return 'true';
			} else {
				// 提示商品删除失败
				return Promise.reject(new Error('Failed to remove the item'));
			}
		},
		// 切换商品选中状态
		async upDataChecked(context, { skuId, isChecked }) {
			let result = await ChangChecked(skuId, isChecked);
			if (result.data.code === 200) {
				return 'true';
			} else {
				return Promise.reject(new Error('Failed to switch status'));
			}
		},
	},
	getters: {
		cartDataList(state) {
			return state.cartDataList[0] || {};
		},
	},
};
